package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final CategoryMapper categoryMapper;

    //分页查询
    public PageResult pageQuerySetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page =setmealMapper.pageQuerySetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增套餐
    @Transactional(rollbackFor = Exception.class)
    public void addSetmeal(SetmealVO setmealVO) {
        //1.新增套餐(setmeal)
        //2.新增stemeal_dish
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        try {
            setmealMapper.addSetmeal(setmeal);
            setmealDishMapper.insertSetmealDishBunch(setmeal.getId(),setmealVO.getSetmealDishes());
        }catch (Exception e){
            log.error(e.getMessage(),e);
            if(e.getClass().equals(DuplicateKeyException.class))
                throw new BaseException(MessageConstant.SETMEAL_NAME_EXIST);
            else throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
    }

    //根据id查询套餐
    public SetmealVO selectSetmealById(Long id) {
        //1.查询套餐
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        //2.查询关联菜品
        setmealVO.setSetmealDishes(setmealDishMapper.selectDishesBySetmealId(id));
        //3.查询分类名称
        setmealVO.setCategoryName(categoryMapper.selectNameById(setmealVO.getCategoryId()));
        return setmealVO;
    }

    //套餐起售、停售
    public void updateSetmealStatus(Integer status, Integer id) {
        if(status == 0){
            setmealMapper.updateSetmealStatus(status,id,BaseContext.getCurrentId());
            return;
        }
        if(setmealDishMapper.isAllOnsell(id)==null){
            setmealMapper.updateSetmealStatus(status,id,BaseContext.getCurrentId());
        }
        else throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
    }

    //修改套餐
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        try {
        setmealMapper.updateSetmeal(setmeal);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            if(e.getClass().equals(DuplicateKeyException.class))
                throw new BaseException(MessageConstant.SETMEAL_NAME_EXIST);
            else throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }
        setmealDishMapper.deleteSetmealDishById(setmealDTO.getId());
        setmealDishMapper.insertSetmealDishBunch(setmealDTO.getId(),setmealDTO.getSetmealDishes());
    }
    //批量删除套餐
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmeal(List<Long> ids) {
        setmealMapper.deleteSetmeal(ids);
        setmealDishMapper.deleteSetmealDishByIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
